package cn.zyl.demo.qle.controller;

import cn.zyl.demo.qle.operator.JoinOperator;
import cn.zyl.demo.qle.tools.SpringExpressTool;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * QleIndexController类说明:
 * <p>
 *  <a href="https://developer.aliyun.com/article/621206">QLExpress功能清单</a>
 * </p>
 * @author z.y.l
 * @version v1.0
 * @date 2020-08-28
 */
@Component
@RestController
@RequestMapping("/")
public class QleIndexController {
    @RequestMapping("")
    public String index(){
        return "Hello Spring Boot QLE!";
    }
    @RequestMapping("hello")
    public String hello(String name) throws Exception {
        ExpressRunner runner = new ExpressRunner();
        DefaultContext<String, Object> context = new DefaultContext<>();
        context.put("a","Spring Boot QLE.");
        context.put("c",name);
        String express = "a+c";
        Object r = runner.execute(express, context, null, true, false);
        return r.toString();
    }
    @RequestMapping("get")
    public Object get()throws Exception{
        Map<String, Object> innerContext = new HashMap<>(2);
        innerContext.put("id", 111);
        String express = "客户信息(id)";
        return SpringExpressTool.execute(express,innerContext);
    }
    @RequestMapping("query")
    public Object query()throws Exception{
        Map<String, Object> innerContext = new HashMap<>(2);
        innerContext.put("name", "zyl-");
        String express = "客户列表(name)";
        return SpringExpressTool.execute(express,innerContext);
    }
    @RequestMapping("save")
    public Object save()throws Exception{
        Map<String, Object> innerContext = new HashMap<>(2);
        innerContext.put("标识", 234234L);
        innerContext.put("名称", "winSonZha");
        innerContext.put("年龄", 23);
        String express = "客户新增(标识,名称,年龄)";
        return SpringExpressTool.execute(express,innerContext);
    }
    @RequestMapping("join")
    public Object join()throws Exception{
        ExpressRunner runner = new ExpressRunner();
        DefaultContext<String, Object> context = new DefaultContext<>();
        runner.addOperator("join",new JoinOperator());
        return runner.execute("1 join 2 join 3", context, null, false, false);
    }
}
